package com.game.controller;

import com.game.model.GameRoom;
import com.game.model.Player;
import com.game.model.Card;
import com.game.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import java.util.List;

@Controller
public class GameController {

    @Autowired
    private GameService gameService;

    @MessageMapping("/createRoom")
    @SendTo("/topic/rooms")
    public GameRoom createRoom() {
        return gameService.createRoom();
    }

    @MessageMapping("/joinRoom")
    @SendTo("/topic/room/{roomId}")
    public GameRoom joinRoom(String roomId, Player player) {
        GameRoom room = gameService.getRoom(roomId);
        if (room != null) {
            room.addPlayer(player);
        }
        return room;
    }

    @MessageMapping("/startGame")
    @SendTo("/topic/room/{roomId}")
    public GameRoom startGame(String roomId) {
        gameService.startGame(roomId);
        return gameService.getRoom(roomId);
    }

    @MessageMapping("/submitHand")
    @SendTo("/topic/room/{roomId}")
    public GameRoom submitHand(String roomId, String playerId, List<Card> frontHand, List<Card> middleHand, List<Card> backHand) {
        gameService.submitHand(roomId, playerId, frontHand, middleHand, backHand);
        return gameService.getRoom(roomId);
    }
} 